package cn.yuan.xiaoyu.testmodule.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by yukuoyuan on 2017/8/26.
 * 这是一个处理权限的工具类
 */
public class PermissionsUtils {
    private static PermissionsUtils sInstance;
    private OnPermissionListener onPermissionListener;

    private PermissionsUtils() {
    }

    public interface OnPermissionListener {
        void onRequestPermissionSuccess(int requestCode);

        void onRequestPermissionFailure(int requestCode);
    }

    public static PermissionsUtils instance() {
        if (sInstance == null) {
            synchronized (PermissionsUtils.class) {
                if (sInstance == null) {
                    sInstance = new PermissionsUtils();
                }
            }
        }
        return sInstance;
    }

    /**
     * 申请权限的方法
     *
     * @param context              当前界面
     * @param permission           需要申请的权限
     * @param requestCode          请求标识
     * @param onPermissionListener 回调监听
     */
    public void requestPermission(@NonNull Activity context, @NonNull String permission, int requestCode, OnPermissionListener onPermissionListener) {
        this.onPermissionListener = onPermissionListener;
        /***
         * 是否是询问权限
         */
        if (ContextCompat.checkSelfPermission(context,
                permission)
                != PackageManager.PERMISSION_GRANTED) {
            /**
             * 权限询问
             */
            ActivityCompat.requestPermissions(context,
                    new String[]{permission},
                    111);
        } else if (ContextCompat.checkSelfPermission(context,
                permission)
                == PackageManager.PERMISSION_DENIED) {
            /**
             * 权限拒绝了
             */
            if (onPermissionListener != null) {
                onPermissionListener.onRequestPermissionFailure(requestCode);
            }
        } else {
            /**
             * 权限允许
             */
            if (onPermissionListener != null) {
                onPermissionListener.onRequestPermissionSuccess(requestCode);
            }
        }
    }

    /**
     * 处理权限回调结果
     *
     * @param requestCode          请求标识
     * @param grantResults         是否允许了权限
     * @param onPermissionListener 回调监听
     */
    public void onRequestPermissionsResult(int requestCode, int[] grantResults, OnPermissionListener onPermissionListener) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (onPermissionListener != null) {
                onPermissionListener.onRequestPermissionSuccess(requestCode);
            }
        } else {
            if (onPermissionListener != null) {
                onPermissionListener.onRequestPermissionFailure(requestCode);
            }
        }
    }


}
